国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ios7之后根據UILabel的文字計算frame的方法

2019-11-14 18:05:51
字體:
來源:轉載
供稿:網友

ios7 新出來的根據label的文字和字體大小來確定label的寬高。

官方的方法是:

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);

其中NSStringDrawingOptions有四個枚舉值:

typedef NS_OPTIONS(NSInteger, NSStringDrawingOptions) { // 如果文本內容超出指定的矩形限制,文本將被截去并在最后一個字符后加上省略號。如果沒有指定NSStringDrawingUsesLineFragmentOrigin選項,則該選項被忽略    NSStringDrawingTruncatesLastVisibleLine = 1 << 5, // Truncates and adds the ellipsis character to the last visible line if the text doesn't fit into the bounds specified. Ignored if NSStringDrawingUsesLineFragmentOrigin is not also set.  // 繪制文本時使用 line fragement origin 而不是 baseline origin    NSStringDrawingUsesLineFragmentOrigin = 1 << 0, // The specified origin is the line fragment origin, not the base line origin  // 計算行高時使用行距。(譯者注:字體大小+行間距=行距)    NSStringDrawingUsesFontLeading = 1 << 1, // Uses the font leading for calculating line heights  // 計算布局時使用圖元字形(而不是印刷字體)。    NSStringDrawingUsesDeviceMetrics = 1 << 3, // Uses image glyph bounds instead of typographic bounds} NS_ENUM_AVAILABLE_IOS(6_0);

 

attributes是文本字體的屬性:該參數要設置字體的大小。

context是上下文對象,用于包含信息:如何調整字間距以及縮放。最終,該對象包含的信息將用于文本繪制。該參數可為 nil。 

 

    NSDictionary *attributes1 = @{NSFontAttributeName:[UIFont systemFontOfSize:20],                                  NSForegroundColorAttributeName:[UIColor redColor]                                  };        UILabel *titleLabel = [UILabel new];    titleLabel.text =  @"德瑪西亞萬歲,斷劍重鑄之日,騎士歸來之時,我們要以困難的方式搞定他。我本可以打的輕一點!";    titleLabel.numberOfLines = 0;//多行顯示,計算高度    titleLabel.textColor = [UIColor blackColor];    titleLabel.backgroundColor = [UIColor greenColor];    CGSize titleSize = [titleLabel.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes1 context:nil].size;    titleLabel.frame = CGRectMake(10, 64, titleSize.width, titleSize.height);        [self.view addSubview:titleLabel];

 

效果圖如下:

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 图木舒克市| 万山特区| 中江县| 宁城县| 湖北省| 灵丘县| 贺州市| 西畴县| 酒泉市| 来凤县| 永定县| 汝阳县| 湖北省| 德昌县| 广东省| 柯坪县| 无极县| 荔波县| 麦盖提县| 青河县| 吴川市| 思茅市| 正镶白旗| 克山县| 交口县| 温泉县| 临朐县| 都江堰市| 桂林市| 定远县| 福鼎市| 松原市| 手游| 菏泽市| 红桥区| 北安市| 临海市| 迭部县| 保靖县| 宁化县| 龙岩市|