Is it possible to make UILabel with non-English characters green if the color mixed layer is turned on?

I have a tabular view with custom cells, and I'm trying to optimize it by making all sublanguages ​​inside the cells green when the Color Blended Layer checked in the simulator.

When the background of the UILabel in the cell is set to white:

 let title = UILabel() contentView.addSubview(title) title.background = UIColor.whiteColor() title.text = "Hi, how are you?" 

This UILabel subtype will turn green in the simulator, which is good. However, if I change the text to some Chinese:

 title.text = "你好" 

The UILabel subroutine will turn red. This post to https://stackoverflow.com/a/146608/212/ provides some explanation of the situation. Is this really a solution?

+6
source share
6 answers

Use CATextLayer instead of UILabel . And don't forget to set its opaque property to true .

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) // You should definitely put layer handling logic into // UITableViewCell subclass. I'm omitting it for clarity let layer = cell.contentView.layer let textLayer = CATextLayer() textLayer.string = "你好" textLayer.bounds = CGRect(x: 0, y: 0, width: 100, height: 50) textLayer.position = CGPoint(x: 50, y: 25) textLayer.opaque = true layer.addSublayer(textLayer) return cell } 

UPD: If the black background is not what you are looking for, the problem becomes a little more complicated, but still solvable.

All you need to do is enter two lines of code into the drawing engine. There are two ways to achieve this:

  • use delegate for CALayer or

  • subclass CATextLayer .

In our case, I think the latter is preferable. Therefore, instead of the CATextLayer in the example below, use this class:

 class OpaqueTextLayerWithCustomizableBackgroundColor: CATextLayer { override func drawInContext(ctx: CGContext) { if let color = backgroundColor { CGContextSetFillColorWithColor(ctx, color) CGContextFillRect(ctx, bounds); } super.drawInContext(ctx) } } 
+3
source

FYI, three steps in XIB (personal preference) or in code.

  • set opaque = YES
  • backgroundColor = solid color
  • clipsToBound = YES

then non-English UILabel will process once as green.

+2
source

I have to say that this is an internal implementation of Apple. And we should not / (perhaps) cannot play with it. After all, the “colors” you see are just a representation of the structure of the layers. The answer to your question is a good explanation.

I would suggest that do not plan for any premature optimizations. In the end, even if you feel an increase in productivity, the “Color Mixed Layer” is ugly and cannot be applied to real devices. Or just try not to pay attention to it!

Edit:

Here is a screenshot from Apple Simulator, if Apple can live with it, you can live with it too! enter image description here

+1
source

You can do it in the old style and just repaint it manually:

 for (CALayer *sublayer in label.layer.sublayers) { //possibly add an if check here for a layer that actually renders text, it may be the case, that you don't want to color one of them sublayer.backgroundColor = label.backgroundColor; } 

You probably have to do this every time iOS really updates colors, maybe never, it can be on every layoutSubviews . You should experiment with what works for you. If it is a cell, it is probably safe in prepareForReuse . If this affects performance too much, just try init or awakeFromNib and expect Apple not to change it on its own.

0
source

Verify that your first letter textlayer.string is English or another

  NSMutableArray*arrAlphabets; arrAlphabets=[NSMutableArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil]] 

Then the Inside Tableview cell for the row using the indexpath method:

 NSString *str=[textlayer.string substringToIndex:1]; if(str isEqualToString:arrAlphabets) //use for loop to run arralphabets { //do your code to maintail the layer color . } 

We hope this concept will help you :)

0
source

Perhaps you can check for monospaced characters (western) and proportional characters (Chinese) depending on the font used.

Monoship characters are 8-bit, and proportional characters are 16-bit. I do not understand how Swift can check for byte sizes, so I cannot provide code.

Apple Swift Programming Guide can help. Check out the "Unicode String Representation" section.

-1
source

All Articles