Uilabel to suppress white space characters such as auto trim spaces and line breaks

Source: Internet
Author: User



Original: Http://www.tuicool.com/articles/AzmaMj



after iOS7.0, the uilabel automatically removes all whitespace characters from the end of the text line, except for the usual half-width spaces (\0x20) and tabs (\ t), and the full-width spaces (\u3000) are also counted, and even extra line breaks (\r,\n) are automatically removed. Although it is convenient to directly assign the control and no value after the trim, but too intelligent, often can not meet some of the simple implementation of the requirements.




Requirements 1. Use the Add \ n method to empty the upper and lower text by two consecutive lines, that is, twice times the text line spacing.
iOS7.0 Workaround: Add a space after each line break
That is, if you want to display:



aaaaaaa
Blank line
Blank line
bbbbbb


Use the following format to assign text values


lbl.text =  @    "Aaaaaaa\n  \u  0020\n  \u  0020bbbbbb "   ; 


Need to increase after iOS7.0, no increase is invalid



lbl.numberOfLines = 0; // 0 means the number of lines is not fixed
             lbl.lineBreakMode=UILineBreakModeWordWrap; // Allow line break (optional) 


Requirement 2. Add a space to the text of all Uilabel and align the text to the right.
iOS7.0 Workaround: Add a space directly after the text, that is, text adds a space before the value is assigned.


Lbl.text= [NSStringstringWithFormat:@"%@%@","aaaaa" ," \u0020" ];


After iOS7.0, you need to rewrite the Uilabel Drawtextinrect method, which is achieved by shortening the width of the default text drawing rect by half a font width.
The specific implementation code is as follows:
File name: MyLabel.h


#import <UIKit/UIKit.h>
            @interface MyLabel : UILabel
            @end


File name: MYLABEL.M


#import "MyLabel.h"
             @implementation MyLabel
             -(id) initWithFrame: (CGRect) frame {
             self = [super initWithFrame: frame];
             if (self) {
                  return self;
             }
             }
             -(void) drawTextInRect: (CGRect) rect {
               // Short the font width by half the font width
               //self.font.pointSize / 2
               return [super drawTextInRect: CGRectMake (rect.origin.x, rect.origin.y, rect.size.width-self.font.pointSize / 2, rect.size.height)];
             }
             @end 


Appendix:   
Uilabel automatically clears the white space character (UNICODE)   
\u0009 CHARACTER tabulation  
\u000a Line feed  
\u000d carriage return  
\u0020 space  
\u0085 NEXT line   
\u00a0 nbsp  
\u1680 ogham SPACE mark  
\u180e Mongolian Vowel separator   
\u2000 EN quad  
\u200a HAIR space  
\u200b ZERO WIDTH space  
\ u2028 line separator  
\u2029 PARAGRAPH separator  
\u202f NARROW no-break space   
\u205f MEDIUM mathematical space  
\u3000 ideographic SPACE



Uilabel to suppress white space characters such as auto trim spaces and line breaks


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.