如何将NSMutableAttributedString设置为两行

时间:2016-03-01 06:28:34

标签: ios uilabel nsdictionary nsmutableattributedstring

我想像这样创建一个require 'aws-sdk' access_key_id = ACCESS_KEY_ID secret_access_key = SECRET_ACCESS_KEY aws3 = AWS::S3.new(access_key_id: access_key_id, secret_access_key: secret_access_key) bucket = aws3.buckets['BUCKET_NAME'] # get data thru iteration bucket.objects.each do |object| puts object.key puts object.read end enter image description here

我想将年份字体设置为HElvaticaNeu-medium,将2015年设置为HelvaticaNeu。我需要这两个字符串出现在2行中,就像在这张图片中一样。

我怎么做到这一点。我以这种方式设置了两个单独的UILablel

NSMutableAttributedString

现在我应该如何将这2个字符串设置为两行? 有没有人有想法?

3 个答案:

答案 0 :(得分:0)

NSMutableAttributedString *nAttrString = [[NSMutableAttributedString alloc] initWithString:@"\n2016" attributes: normalDict];

NSMutableAttributedString  *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString: mAttrString];
[attString appendAttributedString: nAttrString];

// label.attributedText = attString

答案 1 :(得分:0)

你需要将第二个字符串附加到第一个字符串

使用此代码。

UIFont *helMedium = [UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0];
NSDictionary *mediumDict = [NSDictionary dictionaryWithObject: helMedium forKey:NSFontAttributeName];
NSMutableAttributedString *mAttrString = [[NSMutableAttributedString alloc] initWithString:@"Year :" attributes: mediumDict];

UIFont *helNormal = [UIFont fontWithName:@"HelveticaNeue" size:12.0];
NSDictionary *normalDict = [NSDictionary dictionaryWithObject: helNormal forKey:NSFontAttributeName];
NSMutableAttributedString *nAttrString = [[NSMutableAttributedString alloc] initWithString:@"\n2016" attributes: normalDict];

[mAttrString appendAttributedString:nAttrString];

label.attributedText = mAttrString;

答案 2 :(得分:0)

  UIFont *helMedium = [UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0];
  NSDictionary *mediumDict = [NSDictionary dictionaryWithObject: helMedium forKey:NSFontAttributeName];
  NSMutableAttributedString *mAttrString = [[NSMutableAttributedString alloc] initWithString:@"Year :" attributes: mediumDict];

  UIFont *helNormal = [UIFont fontWithName:@"HelveticaNeue" size:12.0];
  NSDictionary *normalDict = [NSDictionary dictionaryWithObject: helNormal forKey:NSFontAttributeName];
  NSMutableAttributedString *nAttrString = [[NSMutableAttributedString alloc] initWithString:@"\n2016" attributes: normalDict];

  [mAttrString appendAttributedString:nAttrString];
  _yearLabel.attributedText = mAttrString;
  [_yearLabel setNumberOfLines:2];

如果您想在UILabel中的文本周围留出更多空间,请指定相同的宽度和高度约束。