संलग्न पाठ केंद्र संरेखण


85

मैंने सब कुछ करने की कोशिश की है लेकिन इस पाठ को केंद्रित नहीं कर सकता। क्या कोई मुझे बता सकता है कि त्रुटि कहाँ है।

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]}];

1
इस लिंक को देखें .... यह आपकी मदद कर सकता है .. stackoverflow.com/questions/6801856/…
EI Captain v2.0

जवाबों:


131

स्विफ्ट 5 में

let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
textView.attributedText = NSAttributedString(string: "String",
                                                     attributes: [.paragraphStyle: paragraph])

स्विफ्ट -4 में

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

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

स्विफ्ट -3 में

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

let attributes: [String : Any] = [NSParagraphStyleAttributeName: paragraph]
let attrString = NSAttributedString(string:"string", attributes: attributes)
textView.attributedText =  attrString

1
NSParagraphStyleAttributeName -> NSAttributedStringKey.paragraphStyle
अभिजित

62

आप इसका उपयोग करके केंद्र संरेखण सेट कर सकते हैं। रेंज सेट करना याद रखें।

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];

38

स्विफ्ट 4 में

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

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

18

दूसरा रास्ता:

स्विफ्ट :

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

ओबज-सी :

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

16

स्विफ्ट में

let titleString = "title here"

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

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

titleAttributedLabel.attributedText = attributedString

15

स्विफ्ट 4+

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center

// Swift 4.2++
let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedString.Key.paragraphStyle:paragraphStyle])

// Swift 4.1--
let attributedString = NSMutableAttributedString(string: "Your String", attributes: [NSAttributedStringKey.paragraphStyle:paragraphStyle])

let yourLabel = UILabel()
yourLabel.attributedText = attributedString

उद्देश्य सी

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;

1
आप ObjC उदाहरण में स्ट्रिंग में विशेषता जोड़ना भूल गए :)
जोर्डन

9

Swift4

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))

उदाहरण


4

स्विफ्ट 2.x में करने के लिए

let attributeString = NSMutableAttributedString(string: "text")
style.alignment = .Center
attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: range)

2

कभी-कभी जब पाठ अरबी या अन्य दाईं ओर वाली भाषाओं में होता है, तो जब संरेखण उचित होता है, तो अंतिम पंक्ति पाठ बाईं ओर समाप्त होता है। इसके लिए हम baseWritingDirection जोड़ सकते हैं नीचे नमूना कोड है

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

1

यदि आप UIButton पर एट्रिब्यूटेड टेक्स्ट सेट करते हैं, तो लाइन ब्रेडमोड सेट करें।

स्विफ्ट 5

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

उद्देश्य सी

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = NSTextAlignmentCenter;
style.lineBreakMode = NSLineBreakByClipping;
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.