您的位置:首页 > 产品设计 > UI/UE

iOS UILabel文字自动换行

2014-11-01 10:18 495 查看
1、文字对齐方式

/* Values for NSTextAlignment */

typedef NS_ENUM(NSInteger, NSTextAlignment) {

    NSTextAlignmentLeft      = 0,   
// Visually left aligned,左对齐

#if TARGET_OS_IPHONE

    NSTextAlignmentCenter    = 1,   
// Visually centered,居中

    NSTextAlignmentRight     = 2,   
// Visually right aligned,右对齐

#else /* !TARGET_OS_IPHONE */

    NSTextAlignmentRight     = 1,   
// Visually right aligned

    NSTextAlignmentCenter    = 2,   
// Visually centered

#endif

    NSTextAlignmentJustified = 3,   // Fully-justified. The last line in a paragraph is natural-aligned,最后一行自然对齐
    NSTextAlignmentNatural   =
4,   // Indicates the default alignment for script,默认对齐脚本

} NS_ENUM_AVAILABLE_IOS(6_0);

2、文字裁剪方式

// NSParagraphStyle

typedef NS_ENUM(NSInteger, NSLineBreakMode) {/* What to do with long lines */

    NSLineBreakByWordWrapping = 0,    
/* Wrap at word boundaries, default */  //以空格为边界,保留单词

    NSLineBreakByCharWrapping,/* Wrap at character boundaries */   //保留整个字符

    NSLineBreakByClipping, /* Simply clip */  //剪切与文本宽度相同的内容长度,后半部分被删除

    NSLineBreakByTruncatingHead,/* Truncate at head of line: "...wxyz" */  //按照"……文字"显示,前面省略,后面显示

    NSLineBreakByTruncatingTail,/* Truncate at tail of line: "abcd..." */  //按照"文字……"显示,前面显示,后面省略

    NSLineBreakByTruncatingMiddle/* Truncate middle of line:  "ab...yz" */  //按照"文字……文字"显示,首尾显示

} NS_ENUM_AVAILABLE_IOS(6_0);

3、代码演示

   //描述文字,自动换行

    UILabel *_desLabel = [[UILabelalloc]initWithFrame:CGRectMake(10,40,
200, 40)];

    _desLabel.text =@"阅尽天涯离别苦,不道归来,零落花如许。花底相看无一语,绿窗春与天俱莫。
待把相思灯下诉,一缕新欢,旧恨千千缕。最是人间留不住,朱颜辞镜花辞树——By
王国维";

    _desLabel.font = [UIFontfontWithName:@"TimesNewRomanPSMT"size:14];

    [_desLabel setNumberOfLines:0]; //numberoflines(即最大行数限制)设置成0,即不做行数限制

    _desLabel.lineBreakMode =NSLineBreakByWordWrapping;

    

    [self.viewaddSubview:_desLabel];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UILabel Objective-C ios