ios - نص مركز محاذاة محاذاة




xcode label (6)

طريق اخر:

سويفت :

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = NSTextAlignmentCenter;    
NSAttributedString *attributedString =  [NSAttributedString.alloc initWithString:@"This will be centered." 
attributes: @{NSParagraphStyleAttributeName:paragraphStyle}];

Obj-C :

let attributedString = NSMutableAttributedString(string: "Example text that is centered using a paragraph style. With the ability to change the width between lines.", attributes: [NSAttributedStringKey.font: GothamFont.medium(with: 14)])

let myParagraphStyle = NSMutableParagraphStyle()
myParagraphStyle.alignment = .center // center the text
myParagraphStyle.lineSpacing = 14 //Change spacing between lines
myParagraphStyle.paragraphSpacing = 38 //Change space between paragraphs
attributedString.addAttributes([.paragraphStyle: myParagraphStyle], range: NSRange(location: 0, length: attributedString.length))

لقد جربت كل شيء ، لكن لا يبدو أنني ركزت على هذا النص. يمكن للشخص من فضلك قل لي أين الخطأ.

NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
paragraphStyle.alignment = NSTextAlignmentCenter;
label.attributedText = [[NSAttributedString alloc] initWithString:cell.EventTitle.text attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],NSParagraphStyleAttributeName:paragraphStyle,NSBaselineOffsetAttributeName : @0,NSFontAttributeName : [UIFont fontWithName:@"BrandonGrotesque-Black" size:34]}];

في بعض الأحيان عندما يكون النص باللغة العربية أو لغات محاذاة إلى اليمين ، ثم عند إجراء محاذاة مضبوطة ، ينتهي نص السطر الأخير في الجانب الأيسر. لهذا يمكننا أن نضيف baseWritingDirection أدناه هو رمز عينة

 let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .justified paragraphStyle.baseWritingDirection = .rightToLeft attribute.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:range) txtView.attributedText = attribute 

للقيام بذلك في سويفت 2.x

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .justified
paragraphStyle.baseWritingDirection = .rightToLeft
attribute.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:range)
txtView.attributedText = attribute

يمكنك ضبط محاذاة الوسط باستخدام هذا. تذكر أن تحدد النطاق.

let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center

textView.attributedText = NSAttributedString(string: "string",
                                         attributes: [.paragraphStyle: paragraph])

سويفت 4+

NSString *string = @"Your String";
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string attributes: @{NSParagraphStyleAttributeName:paragraphStyle}];
UILabel *label = [[UILabel alloc] init];
label.attributedText = attributedString;

ج موضوعية

let paragraphStyle = NSMutableParagraphStyle.init()
paragraphStyle.alignment = .center
let attributedString = NSAttributedString.init(string: "This will be centered.", attributes: [NSParagraphStyleAttributeName: paragraphStyle])

في سويفت 4

let titleString = "title here"

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Center

let attributedString = NSAttributedString(
    string: titleString,
    attributes: [NSParagraphStyleAttributeName: paragraphStyle]
)

titleAttributedLabel.attributedText = attributedString




centering