ios - 如何使 TTTAttributedLabel 垂直对齐中心与截断的尾部
<p><p>我正在使用 <a href="https://github.com/TTTAttributedLabel/TTTAttributedLabel" rel="noreferrer noopener nofollow">TTTAttributedLabel</a>显示一些垂直居中的文本(默认在 <code>TTTAttributedLabel</code> 和 <code>UILabel</code> 中),单行(也是默认),并截断尾行中断。</p>
<pre><code>TTTAttributedLabel *label1 = [ initWithFrame:CGRectMake(20.0, 40.0, 200.0, 60.0)];
label1.backgroundColor = ;
label1.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
;
TTTAttributedLabel *label2 = [ initWithFrame:CGRectMake(20.0, 120.0, 200.0, 60.0)];
label2.backgroundColor = ;
label2.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
;
NSString *shortString = @"This is a short string.";
NSString *longString = @"This is a somewhat longer string. In fact its really long. So long it takes up alot of space.";
NSDictionary *attributes = @{NSFontAttributeName: };
NSMutableAttributedString *shortAttributedString = [ initWithString:shortString attributes:attributes];
label1.attributedText = shortAttributedString;
NSMutableAttributedString *longAttributedString = [ initWithString:longString attributes:attributes];
label2.attributedText = longAttributedString;
</code></pre>
<p>以上代码呈现如下:</p>
<p> <a href="/image/hllqK.png" rel="noreferrer noopener nofollow"><img src="/image/hllqK.png" alt="enter image description here"/></a> </p>
<p>这两个标签的唯一区别是字符串长度。可以看到,第二个字符串没有垂直居中。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>根据提出的类似问题 <a href="https://stackoverflow.com/questions/20350271/how-to-make-tttattributedlabel-align-center-programatically-in-ios" rel="noreferrer noopener nofollow">here</a> ,需要将段落样式<code>lineBreakMode</code>设置为<code>NSLineBreakByTruncatingTail</code>:</p>
<pre><code>TTTAttributedLabel *label1 = [ initWithFrame:CGRectMake(20.0, 40.0, 200.0, 60.0)];
label1.backgroundColor = ;
label1.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
;
TTTAttributedLabel *label2 = [ initWithFrame:CGRectMake(20.0, 120.0, 200.0, 60.0)];
label2.backgroundColor = ;
label2.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
;
NSString *shortString = @"This is a short string.";
NSString *longString = @"This is a somewhat longer string. In fact its really long. So long it takes up alot of space.";
NSMutableParagraphStyle *paragraphStyle = [ init];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
NSDictionary *attributes = @{NSFontAttributeName: ,
NSParagraphStyleAttributeName: paragraphStyle};
NSMutableAttributedString *shortAttributedString = [ initWithString:shortString attributes:attributes];
label1.attributedText = shortAttributedString;
NSMutableAttributedString *longAttributedString = [ initWithString:longString attributes:attributes];
label2.attributedText = longAttributedString;
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何使 TTTAttributedLabel 垂直对齐中心与截断的尾部,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32175946/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32175946/
</a>
</p>
页:
[1]