2016-11-08 82 views
0

我有一個uilabel到我的iOS應用程序。我使用段落樣式將歸因文本應用於UIlabel。但我沒有理解爲什麼理由不適用於第4-5行?然後開始所有打印(見下面的截圖)。請建議我做錯了什麼。沒有得到正確的UILabel歸因文本正確

問題

enter image description here

MY CODE - 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 
+0

嘗試替換[LabelText的長度] [歸於字符串長度]在第二個最後一行 –

+0

@Md易卜拉欣哈桑 - 謝謝。我根據您的評論做了更改。但仍然沒有改變。 – appleBoy21

+0

注意與您的問題有關,但請注意,您不應在理論上獲得正確的字體(請參閱:http://stackoverflow.com/questions/37120535/uitextview-attributed-text-not-working-when-using-自定義字體/ 37132592#37132592) – Larme

回答

1

嘗試此代碼:

NSMutableParagraphStyle *ParagraphStyle = [[NSMutableParagraphStyle alloc] init]; 
    ParagraphStyle.alignment = NSTextAlignmentJustified; 
    ParagraphStyle.firstLineHeadIndent = 0.001; 
    ParagraphStyle.lineSpacing = 5.0; 
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:labeltext attributes:@{NSParagraphStyleAttributeName:ParagraphStyle}]; 
self.labelDetails.AttributedText = attStr; 
+0

謝謝saurabh。正常工作。我保留了我的程序。只需將[_paragraphStyle setFirstLineHeadIndent:0.001]行添加到paragraphStyle的懶惰實例化中。工作很好。再次感謝。但你能解釋什麼是問題嗎? – appleBoy21

+0

@Sanket段落樣式定義的意圖是必要的。 –

+0

謝謝。我牢記在心:) – appleBoy21