macOS 开发问题汇总

目录 11+

11. macOS 调用framework中类别的方法 编译成功但运行报错
12. macOS语言本地化/国际化同iOS一致,参见:iOS语言本地化/国际化一些技巧
13. macOS 打包问题记录
14. macOS 完全清理App
15. xib嵌套
16. macOS 修改App名称
17. macOS App Main Menu 工具栏

目录1-10

1. 浏览器打开URL

  [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.baidu.com"]];

2. NSTextField超链接

//policy
NSFont *font = [NSFont systemFontOfSize:12];
attStr = [[NSMutableAttributedString alloc] initWithString:@"登录即同意《用户协议》和《隐私政策》"];
[attStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, 18)];
[attStr addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithRed:175.0f/255.0f green:174.0f/255.0f blue:174.0f/255.0f alpha:1.0f] range:NSMakeRange(0, 18)];
NSRange range = [[attStr string] rangeOfString:@"《用户协议》"];

//policy-1
[attStr addAttribute:NSFontAttributeName value:font range:range];
[attStr addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithRed:5.0f/255.0f green:164.0f/255.0f blue:255.0f/255.0f alpha:1.0f] range:range];;
[attStr addAttribute:NSLinkAttributeName value:kCFPrivacyTerms_URL range:range];

//policy-2
range = [[attStr string] rangeOfString:@"《隐私政策》"];
[attStr addAttribute:NSFontAttributeName value:font range:range];
[attStr addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithRed:5.0f/255.0f green:164.0f/255.0f blue:255.0f/255.0f alpha:1.0f] range:range];
[attStr addAttribute:NSLinkAttributeName value:kCFPrivacyPolicy_URL range:range];

self.tfPolicy.enabled = YES;
self.tfPolicy.editable = NO;//必须禁止输入,否则点击将弹出输入键盘
self.tfPolicy.selectable = YES;// 超连接
self.tfPolicy.allowsEditingTextAttributes = YES;// 超连接
self.tfPolicy.attributedStringValue = attStr;

3. NSTextField光标颜色 NSTextFieldCell和NSSecureTextFieldCell垂直居中 附加视图

- (void)drawRect:(NSRect)dirtyRect {
    
    [super drawRect:dirtyRect];
    
    // Drawing code here.
    /*无效*/
//    NSView *view = [[NSView alloc] initWithFrame:CGRectMake(0, 0, NSWidth(dirtyRect), NSHeight(dirtyRect))];
//    view.layer.backgroundColor = [NSColor blueColor].CGColor;
//    [self addSubview:view];
}

-(BOOL) becomeFirstResponder {
    
    BOOL res = [super becomeFirstResponder];
    if ( res && self.hadSetInsertionPointColor == NO ) {
        
        NSTextView* textField = (NSTextView*)[self currentEditor];
        if ( [textField respondsToSelector: @selector(setInsertionPointColor:)] ) {
            
            self.hadSetInsertionPointColor = YES;
            [textField setInsertionPointColor:self.insertionPointColor?:self.textColor];
        }
    }
    return res;
}
@end

/**密码输入框NSSecureTextField内容垂直居中问题
 NSSecureTextField-NSSecureTextFieldCell:不垂直居中
 NSSecureTextField-LTSecureTextFieldCell:不垂直居中
 NSTextField-LTSecureTextFieldCell:垂直居中
 
 分类方式:成为第一响应者后,才有效;失去第一响应者后无效
 */
@interface LTSecureTextFieldCell : NSSecureTextFieldCell

/**边界,可以自定义控制视图等*/
@property (nonatomic, assign) NSEdgeInsets edgeInsets;

@end

@implementation LTTextFieldCell

#pragma mark - 1无效
//- (NSRect)titleRectForBounds:(NSRect)rect{
//
//    NSRect titleRect = [super titleRectForBounds:rect];
//    CGFloat minimHeight = self.cellSize.height;
//    titleRect.origin.y += (titleRect.size.height - minimHeight)/2;
//    titleRect.size.height = minimHeight;
//    return titleRect;
//}
//
//-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{
//  [super drawWithFrame:cellFrame inView:controlView];
//}
//
//-(void)selectWithFrame:(NSRect)rect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)delegate start:(NSInteger)selStart length:(NSInteger)selLength{
//
//  CGRect titleRect =  [self titleRectForBounds:rect];
//    titleRect.origin.y += 10;//self.oringeY;
//  [super selectWithFrame:titleRect inView:controlView editor:textObj delegate:delegate    start:selStart length:selLength];
// NSLog(@"titleRect = %@",NSStringFromRect(titleRect));
//
//}


#pragma mark - 2
- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame {
    
    // super would normally draw text at the top of the cell
    NSRect rect = CGRectMake(self.edgeInsets.left, self.edgeInsets.top, NSWidth(frame) - self.edgeInsets.left - self.edgeInsets.right, NSHeight(frame) - self.edgeInsets.top - self.edgeInsets.bottom);
    CGFloat fontSize = self.font.boundingRectForFont.size.height;
    NSInteger offset = floor((NSHeight(rect) - ceilf(fontSize))/2) - 2;
    NSRect centeredRect = NSInsetRect(rect, 0, offset);
    return centeredRect;
}

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event {

     [super editWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] inView:controlView editor:editor delegate:delegate event:event];
}

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)editor delegate:(id)delegate start:(NSInteger)start length:(NSInteger)length {

    [super selectWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] inView:controlView editor:editor delegate:delegate start:start length:length];
}

 - (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view {

     [super drawInteriorWithFrame:[self adjustedFrameToVerticallyCenterText:frame] inView:view];
}


#pragma mark - 3
/**实际内容区域:修改了cell的frame,导致在设置背景色时,部分区域无效*/
//- (NSRect)drawingRectForBounds:(NSRect)rect {
//    
//    NSRect rect1 = [super drawingRectForBounds:rect];
//    NSSize size = [self cellSizeForBounds:rect];
//    CGFloat h = rect1.size.height - size.height;
//    if ( h > 0 ) {
//        rect1.size.height = size.height;
//        rect1.origin.y += h * 0.5;
//    }
//    
//    return rect1;
//}

@end

4. 设置NSTextView失去光标

/**设置NSTextView失去光标*/
[self.tvEmail.window makeFirstResponder:nil];
[self.tvSuggest.window makeFirstResponder:nil];

5. 遮罩层MaskView

定制事件传递

/**遮罩视图
 不调用super方法,阻止了事件的传递;
 当前:拦截了鼠标的右键,左键,又不会影响上层的视图点击*/
@interface LTMaskView : NSView

/**
 / **在superview中根据子视图的identifier匹配LTMaskView,
 设置LTMaskView的部分事件继续传递,不被拦截;并且在superview中也可以设置superview自身事件是否继续传递、拦截;
 * /
 [self.view.subviews enumerateObjectsUsingBlock:^(__kindof NSView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
     
     if ( [@"LTMaskView" isEqualToString:obj.identifier] ) {
         LTMaskView *maskView = (LTMaskView *)obj;
         maskView.allowedEventMask = NSEventMaskLeftMouseDown;
     }
 }];
 
 / **与设置里的自定义鼠标主键无关
 问题:present子视图后,屏蔽了子视图的动作,只有superview的mouseDown事件了
 * /
 [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskLeftMouseDown handler:^NSEvent * _Nullable(NSEvent * _Nonnull aEvent) {

     [self mouseDown:aEvent];
     return nil;/ **事件被拦截,不会下传* /
     return aEvent;/ **事件继续传递* /
 }];
 */
@property (nonatomic, assign) NSEventMask allowedEventMask;

@end

#import "LTMaskView.h"

@implementation LTMaskView

- (instancetype)init {
    
    if ( self = [super init] ) {
        /**tag是只读属性,但可以在xib中设置
         self.tag = 10001;
         */
        self.identifier = NSStringFromClass([self class]);
    
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.identifier = NSStringFromClass([self class]);
    }
    return self;
}

- (instancetype)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.identifier = NSStringFromClass([self class]);
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
}

- (void)mouseDown:(NSEvent *)event {
    
    if ( self.allowedEventMask & NSEventMaskLeftMouseDown ) {
        [super mouseDown:event];
    }
}

- (void)rightMouseDown:(NSEvent *)event {
    
    if ( self.allowedEventMask & NSEventMaskRightMouseDown ) {
        [super rightMouseDown:event];
    }
}

@end

6. 弹框NSAlert

- (void) testForWindow:(NSWindow *) window {
    
    NSAlert *alert = [[NSAlert alloc] init];
    alert.icon = nil;//[NSImage imageNamed:@"icon"];/**nil时,默认APPicon*/
    alert.alertStyle = NSAlertStyleWarning;
    [alert addButtonWithTitle:@"确定"];
//    [alert addButtonWithTitle:@"取消"];/**可以注释*/
//    alert.messageText = @"title";/**可以注释*/
//    alert.informativeText = @"msg";/**可以注释*/
    alert.showsSuppressionButton = NO;/**是否展示Don't ask again*/
    
    /**
     方式1
     使用方式1的时候需要注意的是window不能为nil,
     不要讲NSAlert添加在HomeVC的ViewDidLoad中,此时Window还没创建成功。
     */
    [alert beginSheetModalForWindow:window completionHandler:^(NSModalResponse returnCode) {
        if (returnCode == NSAlertFirstButtonReturn) {
            NSLog(@"确定");
        } else if (returnCode == NSAlertSecondButtonReturn) {
            NSLog(@"取消");
        } else {
            NSLog(@"else");
        }
    }];
    /*! 方式2
    NSModalResponse response = [alert runModal];
    if (response == NSModalResponseStop) {

    }
    if (response == NSModalResponseAbort) {

    }
    if (response == NSModalResponseContinue) {

    }
    */
}

7. NSView tag

在xib或storyboard中可以设置tag;但是在代码里,因为tag为readonly,所以不可以设置tag。

8. layout 无效

[self.layer layoutIfNeeded];
[self.layer layoutSublayers];

9. NSBezierPath 圆角 在drawRect中绘制

https://stackoverflow.com/questions/23094140/nsbezierpath-appendbezierpathwitharcfrompoint-wont-draw
/**
 当前绘图的上下文为空,因此无法绘制。
 即:只有在drawRect函数内部才提供了绘制图形上下文环境,
 不能在非drawRect函数内部通过NSBezierPath或者CGPathRef绘制内容
 */
- (void) addArcToProView {
    
    CGSize size = self.proView.bounds.size;
    CGFloat h = size.height;
    CGFloat w = self.tfPro_w.constant / 0.65f;/*重点,计算*/
    NSBezierPath *thePath = [NSBezierPath bezierPath]; // all drawing instruction goes to thePath.
    /*线条*/
    [NSBezierPath setDefaultLineWidth:5.0];
    [[NSColor clearColor] set];/*必须设置为clearColor,否则会显示边框*/
    [thePath moveToPoint:NSMakePoint(w, h)];
    [thePath lineToPoint:NSMakePoint(0, h)];
    [thePath appendBezierPathWithArcFromPoint:NSMakePoint(0,0)  toPoint:NSMakePoint(h,0) radius:h];
    [thePath lineToPoint:NSMakePoint(w, 0)];
    [thePath lineToPoint:NSMakePoint(w, h)];
    [thePath stroke];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = CGRectMake(0, 0, w, h);
    maskLayer.path = [[LTCGTool tool] getCGPathFromNSBezierPath:thePath];
    self.proView.layer.mask = maskLayer;
}

#pragma mark - NSBezierPath
/// 获取NSBezierPath的CGPath
/// 也可以使用分类的方式实现
/// @param path NSBezierPath
- (CGMutablePathRef)getCGPathFromNSBezierPath:(NSBezierPath *)path {
    
    CGMutablePathRef cgPath = CGPathCreateMutable();
    NSInteger n = [path elementCount];
    
    for (NSInteger i = 0; i < n; i++) {
        NSPoint ps[3];
        switch ([path elementAtIndex:i associatedPoints:ps]) {
            case NSBezierPathElementMoveTo: {
                CGPathMoveToPoint(cgPath, NULL, ps[0].x, ps[0].y);
                break;
            }
            case NSBezierPathElementLineTo: {
                CGPathAddLineToPoint(cgPath, NULL, ps[0].x, ps[0].y);
                break;
            }
            case NSBezierPathElementCurveTo: {
                CGPathAddCurveToPoint(cgPath, NULL, ps[0].x, ps[0].y, ps[1].x, ps[1].y, ps[2].x, ps[2].y);
                break;
            }
            case NSBezierPathElementClosePath: {
                CGPathCloseSubpath(cgPath);
                break;
            }
            default: NSAssert(0, @"Invalid NSBezierPathElement");
        }
    }
    return cgPath;
}

10. NSScrollView

基本用法

//style,overloy会独立出来scroll的部分,背景透明
_scrollView.scrollerStyle = NSScrollerStyleOverlay;
// 水平和垂直的弹性属性
_scrollView.horizontalScrollElasticity = NSScrollElasticityNone;
_scrollView.verticalScrollElasticity = NSScrollElasticityNone;
_scrollView.scrollerInsets = NSEdgeInsetsMake(0, 0, 0, -20);//NSEdgeInsets(top:0, left:0, bottom:0, right:-20)
_scrollView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
_scrollView.backgroundColor = [NSColor clearColor];
_scrollView.drawsBackground = NO;
  1. 设置偏移,也可以给NSScrollView添加extension(Swift)或Category(OC)。
CGFloat pY = 0;
CGFloat h = NSHeight(_scrollView.bounds);
NSView *documentView = self.scrollView.documentView;
if ( NO == documentView.isFlipped ) {
    pY = MAX(NSHeight(_scrollView.bounds), NSHeight(documentView.bounds));
}    
[_scrollView scrollPoint:CGPointMake(0, pY - h + _scrollView.fittingSize.height)];// 设置无效
[documentView scrollPoint:CGPointMake(0, pY)];// 粗略值
[documentView scrollPoint:CGPointMake(0, pY - h + _scrollView.fittingSize.height)];// 更为准确的值
[_scrollView.contentView scrollToPoint:CGPointMake(0, pY - h + _scrollView.fittingSize.height)];// 更为准确的值
  1. NSScrollView与TitleBar重叠时,top会有偏移,下列方法清除偏移。
_scrollView.automaticallyAdjustsContentInsets = NO;
//_scrollView.contentInsets = NSEdgeInsetsMake(-44, 0, 0, 0);// 不用
  1. 自定义MYScrollView:禁止滑动;嵌套,滑动事件下传;
@interface MYScrollView : NSScrollView

@end
@implementation MYScrollView

//- (void)reflectScrolledClipView:(NSClipView *)cView {
// /*禁止滑动失败*/
//}

- (void)scrollWheel:(NSEvent *)event {
    
//    NSLog(@"%@-%s", NSStringFromClass([self class]), __func__);
//    NSLog(@"%s, %@, %@", __func__, self, @(event.phase));
//    [super scrollWheel:event];/*禁止滑动:重载scrollWheel:,并且不调用super,无任何实现*/
    
    /**嵌套,滑动事件下传:NSScrollView内嵌套一个MYScrollView,可以随着外侧的NSScrollView一起滚动*/
    [self.nextResponder scrollWheel:event];
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,163评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,301评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,089评论 0 352
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,093评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,110评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,079评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,005评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,840评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,278评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,497评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,667评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,394评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,980评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,628评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,649评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,548评论 2 352

推荐阅读更多精彩内容