Is it possible to make UILabel with non-English characters green when Color Blended Layer is enabled?

扶醉桌前 提交于 2019-12-05 08:28:52

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 a black background is not what you are looking for, a problem becomes a bit trickier, but yet solvable.

All you need is to inject two lines of code into drawing mechanism. There are two ways to achieve this:

  • to use a delegate of a CALayer or

  • to make a subclass of CATextLayer.

In our case I believe the latter is preferable. So instead of 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)
    }
}

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

  1. set opaque = YES
  2. backgroundColor = a solid color
  3. clipsToBound = YES

then non english UILabel will render-pass once, as green.

I have to say this is the internal implementation of Apple. And we shouldn't / (probably) can't fiddle with it. After all, the 'colors' you see is only the representation of the structure of the layers. The answer on the question you attached is a good explanation.

I would suggest that don't plan any premature optimizations. After all, even if you do feel performance increase, the 'Color Blended Layer' is ugly and cannot be applied for real devices. Or simply try not to pay attention to it!

Edit:

Here is a screenshot from Apple's Simulator, if Apple can live with that, you can live with it too!

Check your textlayer.string first letter is English or some other

 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 Inside Tableview cell for row at 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 .
}

Hope this concept helps you :)

Maybe you could check for monospace characters (western) versus proportional ones (Chinese) depending on the font used.

Monospace characters are 8-bit where proportional ones are 16-bit. I'm at a loss of how Swift could check for byte sizes, so I can't provide any code.

Apple's Swift programming guide can help. Check the section titled: Unicode Representations of Strings.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!