Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios attributedString使用属性";firstLineHeadIndent“;不';无法在UILabel中正确显示_Ios_Objective C_Uilabel_Nsattributedstring - Fatal编程技术网

Ios attributedString使用属性";firstLineHeadIndent“;不';无法在UILabel中正确显示

Ios attributedString使用属性";firstLineHeadIndent“;不';无法在UILabel中正确显示,ios,objective-c,uilabel,nsattributedstring,Ios,Objective C,Uilabel,Nsattributedstring,我试图在UILabel中显示attributedString,并在其属性中添加了“firstLineHeadIndent”属性 有16个字符的“国", 它应该分成两行。但UILabel只在一行中显示14个字符,如图“16个字符不正确”所示 当使用17个字符时,它可以正确显示。谁能告诉我为什么?? 这是我的密码: - (void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] initW

我试图在UILabel中显示attributedString,并在其属性中添加了“firstLineHeadIndent”属性

有16个字符的“国", 它应该分成两行。但UILabel只在一行中显示14个字符,如图“16个字符不正确”所示

当使用17个字符时,它可以正确显示。谁能告诉我为什么?? 这是我的密码:

  - (void)viewDidLoad {
    [super viewDidLoad];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 300, 80)];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    [self.view addSubview: label];

    NSMutableParagraphStyle *paraStyle = [NSMutableParagraphStyle new];
    paraStyle.firstLineHeadIndent = 40;
    paraStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attrs = @{
                            NSParagraphStyleAttributeName:paraStyle,
                            NSFontAttributeName:[UIFont systemFontOfSize:17 weight:UIFontWeightBold],
                            NSKernAttributeName:@.9f
                            };
    label.attributedText = [[NSAttributedString alloc] initWithString:@"国国国国国国国国国国国国国国国国" attributes:attrs];}
16个字符不正确:

使用17个字符正确显示:

更新答案

将此行更改为

   UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 300, 80)];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByCharWrapping;
    label.adjustsFontSizeToFitWidth=YES;
    label.backgroundColor=[UIColor redColor];
    label.textAlignment=NSTextAlignmentLeft;
    [self.view addSubview: label];

    NSMutableParagraphStyle *paraStyle = [NSMutableParagraphStyle new];
    paraStyle.firstLineHeadIndent = 20;
    paraStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attrs = @{
                            NSParagraphStyleAttributeName:paraStyle,
                            NSFontAttributeName:[UIFont systemFontOfSize:16 weight:UIFontWeightBold],
                            NSKernAttributeName:@.9f
                            };
    label.attributedText = [[NSAttributedString alloc] initWithString:@"国国国国国国国国国国国国国国国国" attributes:attrs];
解决方案2:

删除段落:东西


我也遇到了这个问题,我认为这是
UILabel
的一个bug。所以我尝试将其更改为
UITextView
,它成功了。

我认为您可以使用带有透明图像的
NSTextAttachment
来添加额外空间。

仍然面临下面的问题注释。@X.TristanUILabel在添加这两个后没有变化Ententes..minimumFontSize在iOS6.0中被弃用。adjustsFontSizeToFitWidth也不起作用。如果您设置paraStyle.firstLineHeadIndent=15;。它将显示您的标签中心检查我的新解决方案2您可以放置任何长度数据fit@X.TristanI想知道缩进=40时为什么不能正确显示。更改headIndent的值只能解决这个问题,在其他情况下仍然会出现错误的显示。你解决了你的问题
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 300, 80)];
    label.numberOfLines = 0;

 label.text=@"国国国国国国国国国国国国国国国国国国";
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.adjustsFontSizeToFitWidth=YES;
    label.backgroundColor=[UIColor redColor];
    label.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview: label];