【发布时间】:2016-11-08 17:52:56
【问题描述】:
我的 iOS 应用中有一个 uilabel。我将属性文本应用于具有段落样式的 UIlabel。但我不明白为什么前 4-5 行没有应用理由?然后所有打印都很好(见下面的截图)。请提出我做错了什么。
问题
我的代码 - DetailsViewController.m
#import "DetailsViewController.h"
@interface DetailsViewController ()
@property (weak, nonatomic) IBOutlet UILabel *labelDescription;
@property (weak, nonatomic) IBOutlet UILabel *labelDetails;
@property (strong, nonatomic) NSMutableParagraphStyle *paragraphStyle;
@end
@implementation DetailsViewController
#pragma mark - lazy instantiation
- (NSMutableParagraphStyle *)paragraphStyle {
if (!_paragraphStyle) {
_paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[_paragraphStyle setAlignment:NSTextAlignmentJustified];
[_paragraphStyle setHyphenationFactor:1.0f];
[_paragraphStyle setLineSpacing:5.0f];
}
return _paragraphStyle;
}
#pragma mark - view controllers life cycle methods
- (void)viewDidLoad {
[super viewDidLoad];
[self.view layoutIfNeeded];
// updating fonts
[Utils updateLabelFontSize:self.labelDescription ForInitialHeight:20 andInitialSize:18];
[self.labelDetails setFont:[self.labelDetails.font fontWithSize:[self.labelDescription bounds].size.height * 0.85]];
// create labelText to.hFile
NSString *labelText = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
[attributedString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:NSMakeRange(0, [labelText length])];
[self.labelDetails setAttributedText:attributedString];
}
@end
【问题讨论】:
-
尝试在倒数第二行将 [labelText length] 替换为 [attributedString length]
-
@Md Ibrahim Hassan - 谢谢。我根据您的评论进行了更改。但仍然没有变化。
-
与您的问题相关的注意事项,但请注意,理论上您不应该得到正确的字体(参见:stackoverflow.com/questions/37120535/…)
-
@Larme:你注意到,我评论了“[self.labelDetails setFont:[self.labelDetails.font fontWithSize:[self.labelDescription bounds].size.height * 0.85]];” viewDidLoad 中的这一行并替换“NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];”用“NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Gotham-Book" size:([self.labelDescription bounds].size.height * 0.85)] }]; ”。但还是不行
-
@Larme - 为了完成我的工作,我必须将 [_paragraphStyle setFirstLineHeadIndent:0.001] 行添加到 paragraphStyle 的惰性实例化中。
标签: ios objective-c