目录 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;
- 设置偏移,也可以给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)];// 更为准确的值
- NSScrollView与TitleBar重叠时,top会有偏移,下列方法清除偏移。
_scrollView.automaticallyAdjustsContentInsets = NO;
//_scrollView.contentInsets = NSEdgeInsetsMake(-44, 0, 0, 0);// 不用
- 自定义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
网友评论