ios - emoji 在 iOS 的 UILabel 中被剪辑
<p><p>您好,我正在开发聊天应用程序,用户可以在其中向其他用户发送笑脸(表情符号)。对于文本,它的工作完美,但当它包含表情符号时,它是正确的。我已经为此使用了方法</p>
<pre><code>- (CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize {
CGSize constraintSize;
constraintSize.height = MAXFLOAT;
constraintSize.width = width;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
, NSFontAttributeName,
nil];
CGRect frame = [text boundingRectWithSize:constraintSize
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:attributesDictionary
context:nil];
CGSize stringSize = frame.size;
return stringSize;
}
</code></pre>
<p>结果类似于 <a href="/image/rXwHE.png" rel="noreferrer noopener nofollow"><img src="/image/rXwHE.png" alt="image 1"/></a> <a href="/image/Ib9lQ.png" rel="noreferrer noopener nofollow"><img src="/image/Ib9lQ.png" alt="image 2"/></a> <a href="/image/Wrh6l.png" rel="noreferrer noopener nofollow"><img src="/image/Wrh6l.png" alt="image 3"/></a> </p>
<p>从图片中你可以看到我想说什么以及我需要什么。当 UILabel 中有表情符号文本时,我需要在两行之间添加一些内容。任何帮助将不胜感激。提前致谢。</p>
<p>编辑:我已经给出了答案。但是,如果有人对此有一些快速的技巧,那对我来说会很棒。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我已经完成了一项很好的手动操作,现在看起来对我来说很好。我为标签文本添加了行距,它对我很有用。我设置了像 </p> 这样的文本
<pre><code>NSMutableAttributedString* attrString = [ initWithString:];
NSMutableParagraphStyle *style = [ init];
;
[attrString addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, )];
lblChatMSG.attributedText = attrString;
</code></pre>
<p>我获取文本高度的方法是</p>
<pre><code>- (CGSize)getSizeForText:(NSString *)text maxWidth:(CGFloat)width font:(NSString *)fontName fontSize:(float)fontSize {
CGSize constraintSize;
constraintSize.height = MAXFLOAT;
constraintSize.width = width;
NSMutableParagraphStyle * paragraphStyle = [ init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
paragraphStyle.lineSpacing = 4;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
, NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName,
nil];
CGRect frame = [text boundingRectWithSize:constraintSize
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:attributesDictionary
context:nil];
CGSize stringSize = frame.size;
return stringSize;
}
</code></pre>
<p>结果是<a href="/image/En07m.png" rel="noreferrer noopener nofollow"><img src="/image/En07m.png" alt="image result for emoji with good space in lines"/></a> </p></p>
<p style="font-size: 20px;">关于ios - emoji 在 iOS 的 UILabel 中被剪辑,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33821006/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33821006/
</a>
</p>
页:
[1]