viv*_*241 2 nsstring drawrect ios
我创建了一个子视图并实现了自定义绘图的drawRect:方法.如何实现类似于UILabel的行为,如果文本太长而无法放入框架中,则会自动添加省略号(...).
这是代码
- (void)drawRect:(CGRect)rect
{
NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:16.0f], NSForegroundColorAttributeName : [UIColor blackColor]};
[self.sampleText drawInRect:CGRectMake(10.0f, 10.0f, self.frame.size.width - 20.0f, self.frame.size.height - 20.0f) withAttributes:attributes];
}
Run Code Online (Sandbox Code Playgroud)
如果sampleText很长,那么它只会被剪裁以适合指定的rect.如何正确添加"..."?
您需要使用其中一种方法,drawInRect:withAttributes:并使用属性字符串属性来设置线截断样式.
尝试:
NSMutableParagraphStyle *ps = [[NSMutableParagraphStyle alloc] init];
[ps setLineBreakMode:NSLineBreakByTruncatingTail];
[attributes setObject:ps forKey:NSParagraphStyleAttributeName];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1417 次 |
| 最近记录: |