当前位置: 代码迷 >> 综合 >> iOS设置UILabel的行间距
  详细解决方案

iOS设置UILabel的行间距

热度:52   发布时间:2024-01-20 18:38:33.0
  1. NSString *textStr = @"iPhone规定:任何应用想访问麦克风,必须被授权麦克风服务。请进入“设置”->“隐私”->“麦克风”中打开QQ空间的麦克风服务。";  
  2.         UIFont *textFont = [UIFont systemFontOfSize:14];  
  3.         CGSize textSize = [textStr sizeWithFont:textFont  
  4.                               constrainedToSize:CGSizeMake(bounds.size.width - 40, QZONE_SCREEN_HEIGHT)];;  
  5.         UILabel *openMicPrivilegeTipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, textSize.width, textSize.height)];  
  6.         openMicPrivilegeTipsLabel.textColor = DefaultDescriptionText2ColorInDefaultTheme;  
  7.         openMicPrivilegeTipsLabel.text = textStr;  
  8.         openMicPrivilegeTipsLabel.backgroundColor = [UIColor clearColor];  
  9.         openMicPrivilegeTipsLabel.textAlignment = UITextAlignmentLeft;  
  10.         openMicPrivilegeTipsLabel.font = [UIFont systemFontOfSize:14];  
  11.         openMicPrivilegeTipsLabel.numberOfLines = 0;  
  12.           
  13.         // 调整行间距  
  14.         NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textStr];  
  15.         NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];  
  16.         [paragraphStyle setLineSpacing:6];  
  17.         [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [textStr length])];  
  18.         openMicPrivilegeTipsLabel.attributedText = attributedString;  
  19.           
  20.         [_tipsBG addSubview:openMicPrivilegeTipsLabel];  
  21.         [openMicPrivilegeTipsLabel sizeToFit];