2016-12-06 17 views
0

如何在iOS中實現以下設計,可以使用任何控件來實現此目的。我沒有那麼有經驗在本地實現這種設計。它是在本地開發或一種混合技術被用於具有不同彩色背景的文本

請找到下面的圖片以供參考

Sample image

+0

是否確定要更改文本的背景?你可以用更簡單的方法做到這一點,你可以讓你的標籤具有透明的背景色,並且只需在圖層/視圖上設置背景色就可以了,結果就像圖片一樣 - 你可以在IB – ColdSteel

+0

你對紫色和灰色的酒吧感興趣嗎?有很多方法可以做到這一點(例如簡單地調整視圖大小),或者使用不同大小和顏色的標籤「SMS配額:200 SMS」,這可以通過'NSAttributedString'完成 – Larme

+0

我怎樣才能實現兩個標籤和意見@ColdSteel – Vinodh

回答

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

結果將作爲後續

enter image description here

+0

非常感謝你的代碼。我需要改變基礎上,使用 – Vinodh

+0

@Vinodh幀如果答案是很有幫助的,請考慮接受它 – ColdSteel

+0

回答我的問題,意見,如何定位基於數據的顏色? – Vinodh