0
A
回答
0
我做了一個編程示例(「Hello World」的種類)爲您 (它我認爲它更容易理解事情的運作方式 它不是完美的解決方案,它可以做的更好,(性能明智),但我選擇它作爲一個簡單的例子,讓你開始(個人I將使用CALayer
作爲背景)。
所以在這裏:
做一個新的類,如下所示:
MyView.h
#import <UIKit/UIKit.h>
@interface MyView : UIView
@end
MyView.m
#import "MyView.h"
@implementation MyView
- (instancetype)initWithFrame:(CGRect)frame
{
if(self = [super initWithFrame:frame])
{
self.backgroundColor = [UIColor grayColor];
//This rect is in self coordinate system
UIView * viewPurple = [[UIView alloc]initWithFrame:CGRectMake(0, 200, frame.size.width, 50)];
viewPurple.backgroundColor = [UIColor purpleColor];
[self addSubview:viewPurple];
//This rect is in viewPurple's coordinate system
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width/2, 50)];
[label setText:@"Hello World!"];
[label setTextAlignment:(NSTextAlignmentCenter)];
[label setTextColor:[UIColor whiteColor]];
[viewPurple addSubview:label];
//All the frame's rects are in it's `superView`/ parent 's coordinate system.
UIView * viewGreen = [[UIView alloc]initWithFrame:CGRectMake(frame.size.width/2, 200, frame.size.width, 50)];
viewGreen.backgroundColor = [UIColor cyanColor];
[self addSubview:viewGreen];
UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width/2, 50)];
[label2 setText:@"World Hello!"];
[label2 setTextAlignment:(NSTextAlignmentCenter)];
[label2 setTextColor:[UIColor whiteColor]];
[viewGreen addSubview:label2];
}
else
{
//Can't allocate the UIView - quite a rare problem.
}
return self;
}
@end
在您的視圖控制器#import "myView.h"
,並添加這個片段到- (void)viewDidLoad
方法。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MyView * view = [[MyView alloc]initWithFrame:self.view.frame];
[self.view addSubview:view];
}
結果將作爲後續
相關問題
- 1. 文本控制和彩色背景
- 2. 黑色背景和彩色文本的代碼編輯器?
- 3. 帶有彩色背景的不透明文字 - 僅限CSS
- 4. FlotCharts兩個彩色背景
- 5. 呈現在彩色背景
- 6. 兩個彩色背景
- 7. 在SSRS中創建一個具有彩色背景的表格
- 8. 具有相同長度背景色的不同長度行
- 9. 郵政網格佈局上的隨機不同彩色背景
- 10. 將透明度設置爲不同的彩色背景imageMagick
- 11. 製作一排帶方形彩色背景的文本元素
- 12. 不同的背景顏色
- 13. 製作特定的UITableViewCell具有不同的背景顏色
- 14. Qt QComboBox具有不同的背景顏色的每個項目?
- 15. 文本的黑色背景
- 16. 文本的純色背景
- 17. 具有不同重複背景的CSS
- 18. 具有不同背景的ButtonView
- 19. 具有不同背景顏色的Android按鈕
- 20. Android - 具有不同背景顏色的TextView
- 21. ListView中具有不同背景色的項目
- 22. 隨機彩色背景的問題
- 23. Android:背景透明的彩色ListViews
- 24. 如何在彩色文本上剪下背景(僅在CSS中)
- 25. ActionBar文本的背景顏色不變
- 26. CSS - 具有Blurr背景的文本
- 27. 在cocos2d中滾動具有不同第一背景的背景
- 28. UITextView文本背景顏色
- 29. 彩色文本
- 30. 黑色背景上的褪色文本
是否確定要更改文本的背景?你可以用更簡單的方法做到這一點,你可以讓你的標籤具有透明的背景色,並且只需在圖層/視圖上設置背景色就可以了,結果就像圖片一樣 - 你可以在IB – ColdSteel
你對紫色和灰色的酒吧感興趣嗎?有很多方法可以做到這一點(例如簡單地調整視圖大小),或者使用不同大小和顏色的標籤「SMS配額:200 SMS」,這可以通過'NSAttributedString'完成 – Larme
我怎樣才能實現兩個標籤和意見@ColdSteel – Vinodh