NSStringDrawingOptions
文本绘制选项。
enum {
NSStringDrawingTruncatesLastVisibleLine= 1 << 5,
NSStringDrawingUsesLineFragmentOrigin= 1 << 0,
NSStringDrawingUsesFontLeading= 1 << 1,
NSStringDrawingUsesDeviceMetrics= 1 << 3,
};
typedef NSInteger NSStringDrawingOptions;
常量
NSStringDrawingTruncatesLastVisibleLine
如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。如果指定了NSStringDrawingUsesLineFragmentOrigin选项,则该选项被忽略。
NSStringDrawingUsesLineFragmentOrigin
绘制文本时使用 line fragement origin 而不是 baseline origin。
Theorigin specified when drawing the string is the line fragment origin and notthe baseline origin.
NSStringDrawingUsesFontLeading
计算行高时使用行间距。(译者注:字体大小+行间距=行高)
NSStringDrawingUsesDeviceMetrics
计算布局时使用图元字形(而不是印刷字体)。
Usethe image glyph bounds (instead of the typographic bounds) when computinglayout.
-
// 获取字符串的大小 ios7
-
-
- (CGSize)getStringRect:(NSString*)aString
-
{
-
CGSize size;
-
-
NSAttributedString* atrString = [[NSAttributedStringalloc] initWithString:aString];
-
-
NSRange range = NSMakeRange(0, atrString.length);
-
-
NSDictionary* dic = [atrString attributesAtIndex:0 effectiveRange:&range];
-
-
size = [aString boundingRectWithSize:CGSizeMake(237, 200) options:NSStringDrawingUsesLineFragmentOriginattributes:diccontext:nil].size;
-
-
return size;
-
-
}
-
-
-
// 获取字符串的大小 ios6
-
- (CGSize)getStringRect_:(NSString*)aString
-
{
-
CGSize size;
-
-
UIFont *nameFont=[UIFont fontWithName:@"Helvetica" size:13];
-
size=[aString sizeWithFont:nameFont constrainedToSize:CGSizeMake(237, 200) lineBreakMode:NSLineBreakByCharWrapping];
-
return size;
-
}
分类的方式
-
#import "UILabel+StringFrame.h"
-
-
@implementation UILabel (StringFrame)
-
-
- (CGSize)boundingRectWithSize:(CGSize)size
-
{
-
NSDictionary *attribute = @{NSFontAttributeName: self.font};
-
-
CGSize retSize = [self.text boundingRectWithSize:size
-
options:\
-
NSStringDrawingTruncatesLastVisibleLine |
-
NSStringDrawingUsesLineFragmentOrigin |
-
NSStringDrawingUsesFontLeading
-
attributes:attribute
-
context:nil].size;
-
-
return retSize;
-
}
-
-
-
@end
-
// 字符串
-
NSString *str = @"日落时分,沏上一杯山茶,听一曲意境空远的《禅》,心神随此天籁,沉溺于玄妙的幻境里。仿佛我就是那穿梭于葳蕤山林中的一只飞鸟,时而盘旋穿梭,时而引吭高歌;仿佛我就是那潺潺流泻于山涧的一汪清泉,涟漪轻盈,浩淼长流;仿佛我就是那竦峙在天地间的一座山峦,伟岸高耸,从容绵延。我不相信佛,只是喜欢玄冥空灵的梵音经贝,与慈悲淡然的佛境禅心,在清欢中,从容幽静,自在安然。一直向往走进青的山,碧的水,体悟山水的绚丽多姿,领略草木的兴衰荣枯,倾听黄天厚土之声,探寻宇宙自然的妙趣。走进了山水,也就走出了喧嚣,给身心以清凉,给精神以沉淀,给灵魂以升华。";
-
-
// 初始化label
-
UILabel *label = [UILabel new];
-
label.backgroundColor = [UIColor whiteColor];
-
[self.view addSubview:label];
-
-
// label获取字符串
-
label.text = str;
-
-
-
// label获取字体
-
label.font = [UIFont fontWithName:nil size:18];
-
-
// 根据获取到的字符串以及字体计算label需要的size
-
CGSize size = [label boundingRectWithSize:CGSizeMake(320, 0)];
-
-
// 设置无限换行
-
label.numberOfLines = 0;
-
-
// 设置label的frame
-
label.frame = CGRectMake(0.0f, 50.0f, size.width, size.height)
阅读(2345) | 评论(0) | 转发(0) |