1

I need to have different color of a label for iPad and iPhone, I know we can change font size for different size classes but is there any way we can set different color based on size class value

I know there is code solution available but i was wondering if size class could help me in this regard

thanks

Arslan Asim
  • 1,062
  • 9
  • 25

2 Answers2

0

You could have two labels with different colours, and one of them set as Installed only for iPhone size classes and another one set as Installed only for iPad classes.

[EDIT]

Ok, I have no idea why my answer is down-voted. Question didn't mentioned anything about what this label is used for. It's a solution to consider if label is just used as a static text. It's not appropriate if you have to keep reference for this label in a UIViewController (as mentioned by Arslan Asim)

tahavath
  • 204
  • 2
  • 8
  • there will be issue for outlet of that label in this case, you'll have to set 2 variables to keep track of them, not a good solution! – Arslan Asim Jun 23 '16 at 11:14
  • Question didn't mentioned anything about what this label is used for. Why are you down voting me? It's a solution to consider if label is just used as a static text. – tahavath Jun 23 '16 at 11:16
  • down vote was from some other user, i've explained what could be issue in your solution, thanks for your suggestion – Arslan Asim Jun 23 '16 at 11:19
0

You can check to see what type of device the user is using by using this piece of code. If IS_IPAD is true then display the iPad color. Hope this helps.

enum UIUserInterfaceIdiom : Int
{
    case Unspecified
    case Phone
    case Pad
}

struct ScreenSize
{
    static let SCREEN_WIDTH         = UIScreen.mainScreen().bounds.size.width
    static let SCREEN_HEIGHT        = UIScreen.mainScreen().bounds.size.height
    static let SCREEN_MAX_LENGTH    = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
    static let SCREEN_MIN_LENGTH    = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}

struct DeviceType
{
    static let IS_IPHONE_4_OR_LESS  = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
    static let IS_IPHONE_5          = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
    static let IS_IPHONE_6          = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
    static let IS_IPHONE_6P         = UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
    static let IS_IPAD              = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0
}

The check is performmed using this line of code:

if(DeviceType.IS_IPAD)

Ref

Community
  • 1
  • 1
onemillion
  • 612
  • 4
  • 18
  • thanks @onemillion for your brilliant piece of code, but i was finding a way to do this via Stroyboard – Arslan Asim Jun 23 '16 at 11:21
  • No problem sorry for the misunderstanding. In my application I have separate storyboards for the iPad and the iPhone application this could be a solution for that. – onemillion Jun 23 '16 at 11:24
  • Is your code from http://stackoverflow.com/a/29622324/1187415? Please add a reference to the original if you copy code from another resource, otherwise it is considered plagiarism. – Martin R Jun 23 '16 at 11:45
  • Yeah I added a reference, I didn't know you had to do this. I am new to stack overflow. Is what I have now acceptable? – onemillion Jun 23 '16 at 11:50