IOS uilabel settings Align, center, align

Source: Internet
Author: User
Tags uikit

In iOS, the default Uilabel text in the vertical direction can only center alignment, Bo Master to participate in foreign sites. Inherits a new class from the Uilabel, realizes the alignment, the center aligns, the bottom aligns. Details such as the following:

[CPP]View Plaincopy
  1. //   
  2. //MyUILabel.h   
  3. //     
  4. //   
  5. //Created by yexiaozi_007 on 3/4/13.   
  6. //Copyright (c) yexiaozi_007. All rights reserved.   
  7. //   
  8. #import <UIKit/UIKit.h>   
  9. typedef enum
  10. {
  11. Verticalalignmenttop = 0, //default
  12. Verticalalignmentmiddle,
  13. Verticalalignmentbottom,
  14. } VerticalAlignment;
  15. @interface Myuilabel:uilabel
  16. {
  17. @Private
  18. VerticalAlignment _verticalalignment;
  19. }
  20. @property (nonatomic) VerticalAlignment verticalalignment;
  21. @end

[CPP]View Plaincopy
  1. //   
  2. //MYUILABEL.M   
  3. //     
  4. //   
  5. //Created by yexiaozi_007 on 3/4/13.   
  6. //Copyright (c) yexiaozi_007. All rights reserved.   
  7. //   
  8. #import "MyUILabel.h"   
  9. @implementation Myuilabel
  10. @synthesize verticalalignment = Verticalalignment_;
  11. -(ID) initWithFrame: (CGRect) Frame {
  12. if (self = [Super Initwithframe:frame]) {
  13. Self.verticalalignment = Verticalalignmentmiddle;
  14. }
  15. return  Self ;
  16. }
  17. -(void) setverticalalignment: (verticalalignment) VerticalAlignment {
  18. Verticalalignment_ = VerticalAlignment;
  19. [Self setneedsdisplay];
  20. }
  21. -(CGRect) Textrectforbounds: (cgrect) Bounds Limitedtonumberoflines: (Nsinteger) NumberOfLines {
  22. CGRect textrect = [Super Textrectforbounds:bounds limitedtonumberoflines:numberoflines];
  23. Switch (self.verticalalignment) {
  24. Case Verticalalignmenttop:
  25. TEXTRECT.ORIGIN.Y = BOUNDS.ORIGIN.Y;
  26. Break  ;
  27. Case Verticalalignmentbottom:
  28. TEXTRECT.ORIGIN.Y = Bounds.origin.y + bounds.size.height-textrect.size.height;
  29. Break  ;
  30. Case Verticalalignmentmiddle:
  31. //Fall through.   
  32. default :  
  33. TEXTRECT.ORIGIN.Y = Bounds.origin.y + (bounds.size.height-textrect.size.height)/2.0;
  34. }
  35. return  Textrect;
  36. }
  37. -(void) Drawtextinrect: (CGRect) Requestedrect {
  38. CGRect actualrect = [self textrectforbounds:requestedrect limitedToNumberOfLines:self.numberOfLines];
  39. [Super Drawtextinrect:actualrect];
  40. }
  41. @end

When in use:

[CPP]View Plaincopy
  1. Lbl_mylabel = [[Myuilabel alloc] Initwithframe:cgrectmake (20, 50, 150, 600)];
  2. Uicolor *color = [Uicolor colorwithpatternimage:[uiimage imagenamed:@"Halftransparent.png"]]; //Use a translucent picture as the label's background color   
  3. Lbl_mylabel.backgroundcolor = color;
  4. Lbl_mylabel.textalignment = Uitextalignmentleft;
  5. Lbl_mylabel.textcolor = Uicolor.whitecolor;
  6. Lbl_mylabel.linebreakmode = Uilinebreakmodewordwrap;
  7. Lbl_mylabel.numberoflines = 0;
  8. [Lbl_mylabel Setverticalalignment:verticalalignmenttop];
  9. [Self Addsubview:lbl_mylabel];

IOS uilabel settings Align, center, align

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.