2016-11-08 8 views
0

私のiOSアプリケーションには1つのuilabelがあります。段落スタイルでUIlabelに属性付きテキストを適用しました。しかし私は正当化が最初の4-5行に適用されない理由を得られませんでしたか?その後、すべてがうまく印刷されます(下のスクリーンショットを参照)。私が間違っていることを示唆してください。正当なUILabel属性のテキストが正しく表示されない

PROBLEM

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長]を交換してみてください[attributedString length]最後の2行目 –

+0

@Md Ibrahim Hassan - ありがとうございます。私はあなたのコメントごとに変更を加えました。しかし、まだ変化はありません。 – 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の遅延インスタンス化に[_paragraphStyle setFirstLineHeadIndent:0.001]行を追加しました。素晴らしい仕事。再度、感謝します。しかし、何が問題なのか説明できますか? – appleBoy21

+0

@Sanket段落スタイルの定義には行の意図が必要です。ありがとう。 –

+0

ありがとう。私は心に留めていた:) – appleBoy21

関連する問題