Uialertview Message text alignment settings for iOS development ios6.0\ios7.0\ios8.0

Source: Internet
Author: User

Did you find the original code:

#pragma mark-

#pragma mark-alert delegate

-(void) Willpresentalertview: (Uialertview *) Alertview

{

For (UIView *subviewin alertview.subviews)

{

UILabel *tmplabel = (UILabel *) SubView;

Tmplabel.textalignment =nstextalignmentleft;

}

}

iOS7.0 and above can not be used (to show that Apple is increasingly strict with private API management, speculation), if you still want to uialertview the text for it to pay some attention.

If you have this requirement:

1>message information displays left-aligned, such as (iOS6.0 and iOS7.1 display)

2> is centered on the title of the following version of iOS7.0, and the message is left (for example).

We have made a slight change to the above proxy method as follows:

#pragma mark-

#pragma mark-alert delegate

-(void) Willpresentalertview: (Uialertview *) Alertview

{

Because I don't want the title to be left,

Nsinteger labelindex = 1;

It's possible to ios7.0 a version of this method.

For (UIView *subviewin alertview.subviews)

{

if (Floor (nsfoundationversionnumber) <= nsfoundationversionnumber_ios_6_1)

{

if ([SubView iskindofclass: [Uilabelclass]])

{

if (Labelindex > 1)

{

UILabel *tmplabel = (UILabel *) SubView;

Tmplabel.textalignment =nstextalignmentleft;

}

Filter out headings

Labelindex + +;

}

}

}

}

However, this can only take effect in the following version of ios7.0, if it is 8.0, the processing method is not the same, as follows:

-(void) Showalertwithmessage: (NSString *) message

{

8.0

if (Nsfoundationversionnumber > Nsfoundationversionnumber_ios_7_1) {

Uialertcontroller *alertcontroller = [Uialertcontroller alertcontrollerwithtitle:@ "FFFF" message:message Preferredstyle:uialertcontrollerstylealert];

Nsmutableparagraphstyle *paragraphstyle = [[Nsmutableparagraphstyle alloc] init];

Paragraphstyle.linebreakmode = nslinebreakbywordwrapping;

Paragraphstyle.alignment = Nstextalignmentleft;

Line spacing

paragraphstyle.linespacing = 5.0;

Nsdictionary * attributes = @{nsfontattributename: [Uifont systemfontofsize:18.0], Nsparagraphstyleattributename: Paragraphstyle};

nsmutableattributedstring *attributedtitle = [[Nsmutableattributedstring alloc] initwithstring:message];

[Attributedtitle addattributes:attributes range:nsmakerange (0, message.length)];

[Alertcontroller setvalue:attributedtitle forkey:@ "Attributedmessage"];//attributedtitle\attributedmessage

End---

Uialertaction *defaultaction1 = [uialertaction actionwithtitle:@ "Cancel"

Style:uialertactionstyledefault

handler:^ (Uialertaction *action) {

Uitextfield *textfield = alertcontroller.textfields[0];

NSLog (@ "text was%@", textfield.text);

}];

Uialertaction *defaultaction2 = [uialertaction actionwithtitle:@ "OK"

Style:uialertactionstyledefault

handler:^ (Uialertaction *action) {

NSLog (@ "OK btn");

[Alertcontrollerdismissviewcontrolleranimated:yes Completion:nil];

}];

[Alertcontroller Addaction:defaultaction1];

[Alertcontroller Addaction:defaultaction2];

Add TextField

Uiviewcontroller *rootviewcontroller = [Uiapplicationsharedapplication].keywindow.rootviewcontroller;

[Rootviewcontroller Presentviewcontroller:alertcontroller Animated:yes Completion:nil];

}else{

Uialertview *tmpalertview = [[Uialertview alloc] initwithtitle:@ "Test line break"

Message:message

Delegate:self

Cancelbuttontitle:nil

otherbuttontitles:@ "Know", nil];

If your system is greater than or equal to 7.0

if (Floor (nsfoundationversionnumber) > Nsfoundationversionnumber_ios_6_1)

{

Cgsize size = [self.messagestring sizewithfont:[uifont systemfontofsize:15] Constrainedtosize:cgsizemake (240, 1000) Linebreakmode:nslinebreakbytruncatingtail];

UILabel *textlabel = [[UILabel alloc] Initwithframe:cgrectmake (0, 0, size.height)];

Textlabel.font = [Uifont systemfontofsize:15];

Textlabel.textcolor = [Uicolor blackcolor];

Textlabel.backgroundcolor = [Uicolor Clearcolor];

Textlabel.linebreakmode = nslinebreakbywordwrapping;

Textlabel.numberoflines = 0;

Textlabel.textalignment = Nstextalignmentleft;

Textlabel.text = self.messagestring;

[Tmpalertview setvalue:textlabel forkey:@ "Accessoryview"];

This place, don't forget to set the Alertview message to empty.

Tmpalertview.message = @ "";

}

[Tmpalertview show];

}

}

That would be all.

Call this method:

-(void) viewdidload

{

[Superviewdidload];

Self.view.backgroundColor = [Uicolorgraycolor];

self.messagestring [email protected] "1. The first line I was 3 son \n2. The second line I was a few words anyway the purpose is to differentiate from the first line \n3. haha i am the foil";

UIButton *alertbtn = [[Uibuttonalloc] Initwithframe:cgrectmake (0,200, 320, 40)];

[Alertbtn settitle:@ "point me, I will alert" forstate:uicontrolstatenormal];

Alertbtn.backgroundcolor = [Uicolorredcolor];

[Alertbtn addtarget:selfaction: @selector (alertbtntapped) forcontrolevents:uicontroleventtouchupinside];

[Self.viewaddSubview:alertBtn];

}

Don't forget to declare an attribute self.messagestring

Uialertview Message text alignment settings for iOS development ios6.0\ios7.0\ios8.0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.